package com.td.filemanager.business.filetype;

import com.td.filemanager.ResourceTable;
import com.td.filemanager.provider.ComponentViewHolder;
import com.td.filemanager.provider.ListComponentProvider;
import com.td.library.data.FileTypeData;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.List;

import static com.td.library.config.FileType.DOCUMENT;
import static com.td.library.config.FileType.IMAGE;

/**
 * 文件类型提供者
 */
public class FileTypeDataProvider extends ListComponentProvider<FileTypeData> {
    /**
     * 实例化新的设备数据提供程序。
     *
     * @param context 上下文
     * @param listBean 列表数据
     * @param resourceId 资源id
     */
    public FileTypeDataProvider(Context context, List<FileTypeData> listBean, int resourceId) {
        super(context, listBean, resourceId);
    }

    @Override
    protected void onItemDataBind(ComponentViewHolder componentViewHolder, FileTypeData item, int position) {
        Text deviceText = (Text) componentViewHolder.getChildComponent(ResourceTable.Id_item_image_title);
        deviceText.setText(item.getFileType().getTypeName());
        Text infoText = (Text) componentViewHolder.getChildComponent(ResourceTable.Id_item_image_info);
        infoText.setText("总数量：" + item.getNumber());

        setDeviceImg(componentViewHolder, item);

        Checkbox check = (Checkbox) componentViewHolder.getChildComponent(ResourceTable.Id_check);
        check.setChecked(item.isChecked());
        check.setCheckedStateChangedListener((absButton, checked) -> {
            item.setChecked(checked);
        });
    }


    private void setDeviceImg(ComponentViewHolder componentViewHolder, FileTypeData item) {
        int imageTypeId = -1;
        switch (item.getFileType()) {
            case DOCUMENT:
                imageTypeId = ResourceTable.Media_icon_file;
                break;
            case IMAGE:
                imageTypeId = ResourceTable.Media_icon_img;
                break;
            case AUDIO:
                imageTypeId = ResourceTable.Media_icon_music;
                break;
            case VIDEO:
                imageTypeId = ResourceTable.Media_icon_video;
                break;
            case OTHER:
                imageTypeId = ResourceTable.Media_icon_other;
                break;
            default:
                break;
        }
        if (imageTypeId != -1) {
            Image image = (Image) componentViewHolder.getChildComponent(ResourceTable.Id_item_type);
            image.setPixelMap(imageTypeId);
        }
    }

}
